﻿using MediatR;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.Commands.Technologys
{
    public class WorkCenterShowHandler : IRequestHandler<WorkCenterShowCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        /// <summary>
        /// 工作中心仓储
        /// </summary>
        private readonly IBaseRepositoryGet<WorkCenter> workCenterRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<WorkCenterShowHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="workCenterRepository"></param>
        /// <param name="logger"></param>
        public WorkCenterShowHandler(IBaseRepositoryGet<WorkCenter> workCenterRepository, ILogger<WorkCenterShowHandler> logger)
        {
            this.workCenterRepository = workCenterRepository;
            this.logger = logger;
        }

        public async Task<Result> Handle(WorkCenterShowCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            string sql = "SELECT * FROM workCenters";
            try
            {
                logger.LogInformation("查询工作中心");
                var list = workCenterRepository.GetAll(sql, default).ToList();
                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = list;
                    result.Msg = "显示成功";
                }

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("查询工作中心失败", ex.Message);
                throw;
            }
        }



    }
}
